﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Globalization
Imports System.Runtime.InteropServices

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class ThemeTypeConverter
        Inherits ExpandableObjectConverter
        ' Methods
        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            If ((destinationType Is GetType(String)) AndAlso (Not context.PropertyDescriptor Is Nothing)) Then
                Return String.Empty
            End If
            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function

        Public Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
            Return MyBase.GetProperties(context, value, attributes).Sort(New PropertyDescriptorSorter)
        End Function


        ' Nested Types
        Private NotInheritable Class PropertyDescriptorSorter
            Implements IComparer
            ' Methods
            Private Function System.Collections.IComparer.Compare(ByVal obj1 As Object, ByVal obj2 As Object) As Integer Implements IComparer.Compare
                Dim descriptor As PropertyDescriptor = [TryCast](obj1,PropertyDescriptor)
                Dim descriptor2 As PropertyDescriptor = [TryCast](obj2,PropertyDescriptor)
                Dim attribute As DispIdAttribute = [TryCast](descriptor.Attributes.Item(GetType(DispIdAttribute)),DispIdAttribute)
                Dim attribute2 As DispIdAttribute = [TryCast](descriptor2.Attributes.Item(GetType(DispIdAttribute)),DispIdAttribute)
                If (attribute Is Nothing) Then
                    Return 1
                End If
                If (attribute2 Is Nothing) Then
                    Return -1
                End If
                Return (attribute.Value - attribute2.Value)
            End Function

        End Class
    End Class
End Namespace

